<?php
/**
 * Created by PhpStorm.
 * User: lzy
 * Date: 2018/11/1
 * Time: 15:46
 */

namespace App\Weixin\Models;

use App\Admin\Contracts\MemberInterface;
use App\Base\Models\BaseModel;
use App\Weixin\Contracts\UserInterface;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * 微信提现
 * Class WeixinUserModel
 * @package App\Weixin\Models
 */
class  UserWithdrawLogModel extends BaseModel
{

    use SoftDeletes;
    /**
     * 表名
     * @var string
     */
    public $table = "user_withdraw_log";
    /**
     * 时间自动
     * @var bool
     */
    public $timestamps = true;
    /**
     * The name of the "created at" column.
     *
     * @var string
     */
    const CREATED_AT = 'create_time';  //修改添加时间字段
    /**
     * The name of the "updated at" column.
     *
     * @var string
     */
    const UPDATED_AT = 'update_time';

    /**
     * 删除
     */
    const DELETED_AT = 'delete_time';
    /**
     * 删除
     */
    // const DELETED_AT = 'delete_time';
    /**
     * 模型的日期字段保存格式。
     */
    protected $dateFormat = 'U';

//    /**
//     * 自动添充值属性
//     * @var array
//     */
//    protected $attributes = [
//        'uid' => AUID,
//    ];

    /**
     * 提现减去
     */
    const  BALANCE_TYPE_1 = 1;

    /**
     * 提现拒绝退回
     */
    const  BALANCE_TYPE_2 = 2;

    protected $appends = ["mobile", "nickname"];

    /**
     * @return MemberInterface
     */
    public function getMemberInterface()
    {
        return app(MemberInterface::class);
    }
    /**
     * @return UserInterface
     */
    public function getUserInterface()
    {
        return app(UserInterface::class);
    }

    /**
     * 完成订单
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function user(){
        return $this->hasOne(WeixinUserModel::class,"uid","uid");
    }
    /**
     * 手机号
     * @return mixed|string
     */
    public function getMobileAttribute()
    {
        $mobile = $this->getMemberInterface()->getMobileByUid($this->uid);
        return $mobile ?? "";
    }

    /**
     * 用户昵称
     * @return string
     */
    public function getNicknameAttribute()
    {
        $info=$this->getUserInterface()->getUserInfoById($this->uid);
        return $info['nickname'] ?? "";
    }

    /**
     * 算出手续费
     * @param $amount
     */
    public function getPoundageByAmount($amount){
        $poundage=0;
        $one=get_config_param_value("POUNDAGE_MINIMUN_AMOUNT");
        $two=get_config_param_value("POUNDAGE_MINIMUN_AMOUNT_2");
        $max=get_config_param_value("POUNDAGE_MINIMUN_AMOUNT_MAX");
        if(!empty($one)){
            $firtParam=explode("/",$one);
            if(empty($firtParam[1])){
                $firtParam=[];
                $firtParam[0]=0;
                $firtParam[1]=0;
            }
        }else{
            $firtParam=[];
            $firtParam[0]=0;
            $firtParam[1]=0;
        }
        if(!empty($two)){
            $twoParam=explode("/",$two);
            if(empty($twoParam)){
                $twoParam=[];
                $twoParam[0]=0;
                $twoParam[1]=0;
            }
        }else{
            $twoParam=[];
            $twoParam[0]=0;
            $twoParam[1]=0;
        }
        if(!empty($max)){
            $maxParam=explode("/",$one);
            if(empty($maxParam)){
                $maxParam=[];
                $maxParam[0]=0;
                $maxParam[1]=0;
            }
        }else{
            $maxParam=[];
            $maxParam[0]=0;
            $maxParam[1]=0;
        }
       switch ($amount){
           case $amount<$firtParam[0]:
               $poundage=$firtParam[1];
               break;
           case $amount<$twoParam[0]:
               $poundage=$twoParam[1];
               break;
           case $amount>=$maxParam[0]:
               $poundage=$maxParam[1];
               break;
       }
       return $poundage;
    }



}